const con = require("../../utils/data.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeNames: [''],
        notice: [],
        active: 0
    },
    onChange(event) {
        this.setData({
            activeNames: event.detail
        });
    },
    orderDetail: function(e) {
        var orderId = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/pages/order-details/index?id=" + orderId
        })
    },
    cancelOrderTap: function(e) {
        var that = this;
        var orderId = e.currentTarget.dataset.id;
        console.log(orderId);
        wx.showModal({
            title: '确定要取消该订单吗？',
            content: '',
            success: function(res) {
                if (res.confirm) {
                    //取消订单接口
                    wx.request({
                            // url: con.index_getread,
                            url: 'https://www.brvpmusic.com/api/orderProduct/cancelOrder',
                            method: 'GET',
                            data: {
                                'oNumber': orderId,
                            },
                            header: {
                                'Content-Type': 'application/json'
                            },
                            success: function(res) {
                                console.log(res.data);
                                console.log('订单取消成功');
                                wx.showToast({
                                    title: '订单取消成功',
                                })
                                that.getlist('');
                            },
                            fail: function(res) {
                                console.log('返回失败');
                            }
                        })
                        // WXAPI.orderClose(orderId, wx.getStorageSync('token')).then(function(res) {
                        //   if (res.code == 0) {
                        //     that.onShow();
                        //   }
                        // })
                }
            }
        })
    },
    toPayTap: function(e) {
        const that = this;
        var oNumber = e.currentTarget.dataset.id;
        var money = e.currentTarget.dataset.money;
        // const needScore = e.currentTarget.dataset.score;
        console.log(e);
        wx.request({
                url: 'https://www.brvpmusic.com/pay/xiadan',
          // url: 'http://3s.dkys.org:12457/pay/xiadan',
                data: {
                    'openid': wx.getStorageSync('OpenId'),
                    'money': money,
                    'title': '下单支付',
                },
                method: 'GET',
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    console.log(res.data);
                    console.log('返回成功');
                    var prepay_id = JSON.parse(res.data.body).prepay_id;
                    var outTradeNo = JSON.parse(res.data.body).out_trade_no;
                    that.setData({
                            prepay_id: prepay_id,
                            outTradeNo: outTradeNo,
                        })
                        // 签名地址
                    wx.request({
                        url: 'https://www.brvpmusic.com/pay/sign',
                      // url: 'http://3s.dkys.org:12457/pay/sign',
                        data: {
                            'prepay_id': that.data.prepay_id,
                        },
                        method: 'GET',
                        header: {
                            'Content-Type': 'application/json'
                        },
                        success: function(res) {
                            console.log(res.data);
                            console.log('返回成功');
                            console.log(res.data.body)
                            var timeStamp = JSON.parse(res.data.body).timeStamp;
                            var nonceStr = JSON.parse(res.data.body).nonceStr;
                            var packages = JSON.parse(res.data.body).package;
                            var paySign = JSON.parse(res.data.body).paySign;
                            // 微信支付
                            wx.requestPayment({
                                timeStamp: timeStamp,
                                nonceStr: nonceStr,
                                package: packages,
                                signType: 'MD5',
                                paySign: paySign,
                                success(res) {
                                    console.log(res)
                                    var errMsg = res.errMsg;
                                    // 下单支付地址
                                    wx.request({
                                        // url: 'http://3s.dkys.org:12457/api/orderProduct/payment',
                                        url: 'https://www.brvpmusic.com/api/orderProduct/payment',
                                        data: {
                                            'oNumber': oNumber,
                                            'money': money,
                                            'openid': wx.getStorageSync('OpenId'),
                                            'errMsg': errMsg,
                                            'outTradeNo': that.data.outTradeNo
                                        },
                                        method: 'GET',
                                        header: {
                                            'Content-Type': 'application/json'
                                        },
                                        success: function(res) {
                                            console.log(res.data);
                                            var donation = res.data.body;
                                            console.log('返回成功donation'+donation.donationMoney);
                                            if(donation!=null){
                                                wx.showToast({
                                                    title: '支付成功，感谢您为爱心基金捐赠'+donation.donationMoney+'元',
                                                    icon:'none',
                                                    duration: 1000
                                                });
                                            }else{
                                                wx.showToast({
                                                    title: '支付成功!',
                                                    icon:'none',
                                                    duration: 1000
                                                });
                                            }

                                            setTimeout(() => {
                                                that.getlist('');
                                            }, 1000);

                                        },
                                        fail: function(res) {
                                            console.log('返回失败');
                                        }
                                    })
                                },
                                fail(res) {
                                    wx.showToast({
                                        title: '您取消了支付'
                                    })
                                }
                            })
                        },
                        fail: function(res) {
                            console.log('返回失败');
                        }
                    })
                },
                fail: function(res) {
                    console.log('返回失败');
                }
            })
            // WXAPI.userAmount(wx.getStorageSync('token')).then(function(res) {
            //   if (res.code == 0) {
            //     // 增加提示框
            //     if (res.data.score < needScore) {
            //       wx.showToast({
            //         title: '您的积分不足，无法支付',
            //         icon: 'none'
            //       })
            //       return;
            //     }
            //     let _msg = '订单金额: ' + money +' 元'
            //     if (res.data.balance > 0) {
            //       _msg += ',可用余额为 ' + res.data.balance +' 元'
            //       if (money - res.data.balance > 0) {
            //         _msg += ',仍需微信支付 ' + (money - res.data.balance) + ' 元'
            //       }          
            //     }
            //     if (needScore > 0) {
            //       _msg += ',并扣除 ' + money + ' 积分'
            //     }
            //     money = money - res.data.balance
            //     wx.showModal({
            //       title: '请确认支付',
            //       content: _msg,
            //       confirmText: "确认支付",
            //       cancelText: "取消支付",
            //       success: function (res) {
            //         console.log(res);
            //         if (res.confirm) {
            //           that._toPayTap(orderId, money)
            //         } else {
            //           console.log('用户点击取消支付')
            //         }
            //       }
            //     });
            //   } else {
            //     wx.showModal({
            //       title: '错误',
            //       content: '无法获取用户资金信息',
            //       showCancel: false
            //     })
            //   }
            // })
    },
    _toPayTap: function(orderId, money) {
        const _this = this
        if (money <= 0) {
            // 直接使用余额支付
            WXAPI.orderPay(orderId, wx.getStorageSync('token')).then(function(res) {
                _this.onShow();
            })
        } else {
            wxpay.wxpay('order', money, orderId, "/pages/order-list/index");
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getlist(options)
    },
    getlist(options) {
        let that = this;
        let openid = wx.getStorageSync('OpenId');
        wx.showLoading({
            mask: true,
            title:'数据加载中...',
        });
        wx.request({
            // url: con.index_getread,
            url: 'https://www.brvpmusic.com/api/orderProduct/getList',
            // url: 'http://192.168.0.104:8080/api/orderProduct/getList',
            method: 'GET',
            data: {
                'openid': wx.getStorageSync('OpenId')
                    // 'oOrderstatus': 0,
            },
            header: {
                'Content-Type': 'application/json'
            },
            success: function(res) {
                console.log(res.data);
                wx.hideLoading()
                console.log('订单返回成功');
                that.setData({
                    myOrderList: res.data.body,
                })
            },
            fail: function(res) {
                wx.hideLoading()
                console.log('返回失败');
            }
        })
    },
    confirmReceipt: function(e) {
        var that = this;
        var orderId = e.currentTarget.dataset.id;
        console.log(orderId);
        wx.showModal({
            title: '确定要确认收货吗？',
            content: '',
            success: function(res) {
                if (res.confirm) {
                    //取消订单接口
                    wx.request({
                            // url: con.index_getread,
                            url: 'https://www.brvpmusic.com/api/orderProduct/confirmReceipt',
                            method: 'GET',
                            data: {
                                'oNumber': orderId,
                            },
                            header: {
                                'Content-Type': 'application/json'
                            },
                            success: function(res) {
                                console.log(res.data);
                                console.log('确认收货成功');
                                wx.showToast({
                                    title: '确认收货成功',
                                })
                                that.getlist('');
                            },
                            fail: function(res) {
                                console.log('返回失败');
                            }
                        })
                        // WXAPI.orderClose(orderId, wx.getStorageSync('token')).then(function(res) {
                        //   if (res.code == 0) {
                        //     that.onShow();
                        //   }
                        // })
                }
            }
        })
    },
    getLogistics: function(e) {
        var that = this;
        var oid = e.currentTarget.dataset.id;
        var address = e.currentTarget.dataset.addr;
        var headpic = e.currentTarget.dataset.headpic;
        console.log(oid);
        wx.showModal({
            title: '确定要加载物流吗？',
            content: '',
            success: function(res) {
                if (res.confirm) {
                    //加载物流
                    wx.navigateTo({
                        // url: con.index_getread,
                        url: '/pages/wuliu/wuliu?oid=' + oid + '&address=' + address + '&headpic=' + headpic,
                    })
                }
            }
        })
    },
    toEvalute: function(e) {
        console.log(e.currentTarget.dataset.onumber);
        var onumber = e.currentTarget.dataset.onumber;
        var pid = e.currentTarget.dataset.pid;
        wx.navigateTo({
            url: '/pages/evalute/comment?onumber=' + onumber + '&pid=' + pid
        })
    },
    toRefund: function(e) {
        console.log(e.currentTarget.dataset.onumber);
        var onumber = e.currentTarget.dataset.onumber;
        wx.navigateTo({
            url: '/pages/refund/index?onumber=' + onumber
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function(options) {
        this.setData({
            active: parseInt(wx.getStorageSync('ordersIndextype'))
        })
        this.getlist('')
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})